const Sequelize = require('sequelize');
module.exports = function (sequelize, DataTypes) {
  return sequelize.define('m_autocode_rules', {
    id: {
      autoIncrement: true,
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true,
      comment: '编码id'
    },
    tenant_id: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: 0,
      comment: '租户id，0表示公用'
    },
    code: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '识别id',
      unique: 'idx_code'
    },
    key_table: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '表名'
    },
    key_field: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '字段'
    },
    status: {
      type: DataTypes.SMALLINT,
      allowNull: true,
      defaultValue: 1,
      comment: '自动编码是否有效,0 禁用 1有效'
    },
    prefix: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '编码前缀，宏变量{{date}}代表6位日期'
    },
    length: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 2,
      comment: '自动增长位数'
    },
    unique_field: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '查重字段配置,多字段组合用逗号'
    }
  }, {
    sequelize,
    tableName: 'm_autocode_rules',
    timestamps: false,
    freezeTableName: true,
    indexes: [
      {
        name: 'PRIMARY',
        unique: true,
        using: 'BTREE',
        fields: [
          { name: 'id' }
        ]
      },
      {
        name: 'idx_code',
        unique: true,
        using: 'BTREE',
        fields: [
          { name: 'code' }
        ]
      },
      {
        name: 'idx_tf',
        using: 'BTREE',
        fields: [
          { name: 'key_table' },
          { name: 'key_field' }
        ]
      },
      {
        name: 'idx_tenant_id',
        using: 'BTREE',
        fields: [
          { name: 'tenant_id' }
        ]
      }
    ]
  });
};
